package cn.keniykey.mc.landterritory.vault;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

import net.milkbowl.vault.permission.Permission;

public final class PermVault extends Perm{
	private final net.milkbowl.vault.permission.Permission perm;
	
	PermVault() {
		RegisteredServiceProvider<Permission> vault = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
		this.perm = vault.getProvider();
	}
	
	@Override
	public boolean isEnable() {
		return (this.perm != null && this.perm.isEnabled());
	}
	@Override
	public boolean hasPermission(String world, OfflinePlayer p, String per) {
		if (per == null || per.isEmpty()) return true;
		if (!this.isEnable()) {
			Player pl = p.getPlayer();
			if (pl == null) return false;
			return pl.hasPermission(per);
		}
		return this.perm.playerHas(world, p, per);
	}
	@Override
	public boolean addPlayerPermission(String world, OfflinePlayer p, String per) {
		if (!this.isEnable()) return false;
		return this.perm.playerAdd(world, p, per);
	}
	@Override
	public boolean removePlayerPermission(String world, OfflinePlayer p, String per) {
		if (!this.isEnable()) return false;
		return this.perm.playerRemove(world, p, per);
	}
	@Override
	public boolean addGroupPermission(String world, String group, String per) {
		if (!this.isEnable()) return false;
		return this.perm.groupAdd(world, group, per);
	}
	@Override
	public boolean removeGroupPermission(String world, String group, String per) {
		if (!this.isEnable()) return false;
		return this.perm.groupRemove(world, group, per);
	}
}